package net.dodoman.ibatis;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * ibatis的事务管理器，目前只支持三种：JDBC,JTA,EXTERNAL JDBC:通过传统的JDBC
 * CONNECTION.COMIT/rollback实现事务支持 JTA:使用容器提供的JTA服务实现全局事务管理
 * EXTERNAL:外部事务管理，如EJB中使用IBATIS，通过EJB的部署配置即可实现自动的事务管理机制
 * 。此时IBATIS将把所有的事务委托给外部容器进行管理
 */
public class IbatisClient {
	private static SqlMapClient sqlmapclient = SqlMapConf.getInstance();

	/**
	 * 根据主健ID修改NAME
	 * 
	 * @param id
	 * @param name
	 */
	public static void updateAuthor(int id, String name) {
		Author author = new Author();
		author.setId(id);
		author.setName(name);
		try {
			// 事务开始，用的是JDBC的事务管理
			sqlmapclient.startTransaction();
			sqlmapclient.update("updateAuthor", author);
			sqlmapclient.commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			System.err.println("修改错误~~");
		} finally {
			try {
				sqlmapclient.endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 查询所有的记录，返回一个集合
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Author> findAll() {
		List<Author> list = null;
		try {
			sqlmapclient.startTransaction();
			// 0:设置从第几条记录开始,1：设置显示记录记录
			// list=sqlmapclient.queryForList("getAllAuthor",null,0,1);
			list = sqlmapclient.queryForList("getAllAuthor", null);
			sqlmapclient.commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			System.err.println("查询错误~~");
		} finally {
			try {
				sqlmapclient.endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	/**
	 * 添加操作
	 * 
	 * @param id
	 * @param name
	 * @return
	 */
	public static boolean insert(int id, String name) {
		boolean bool = false;
		Author author = new Author();
		author.setId(id);
		author.setName(name);
		try {
			sqlmapclient.startTransaction();
			sqlmapclient.insert("insertAuthor", author);
			bool = true;
			sqlmapclient.commitTransaction();
		} catch (SQLException e) {
			bool = false;
			e.printStackTrace();
			System.err.println("添加错误~~");
		} finally {
			try {
				sqlmapclient.endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return bool;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	public static boolean delete(int id) {
		boolean bool = false;
		Author author = new Author();
		author.setId(id);
		try {
			sqlmapclient.commitTransaction();
			sqlmapclient.delete("deleteAuthor", author);
			bool = true;
			sqlmapclient.startTransaction();
		} catch (SQLException e) {
			bool = false;
			e.printStackTrace();
			System.out.println("删除错误~~");
		} finally {
			try {
				sqlmapclient.endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return bool;
	}

	/**
	 * 
	 * @param statamenId
	 * @return
	 */
	public static List selectBySId(String statamenId) {
		return null;
	}

	public static void main(String str[]) {
		// boolean bool=IbatisClient.delete(3);
		// boolean bool=IbatisClient.insert(3,"wanwu");
		// IbatisClient.updateAuthor(3,"jj");
		List<Author> list = IbatisClient.findAll();
		for (Author author : list) {
			System.out.println("ID =" + author.getId());
			System.out.println("NAME =" + author.getName());
		}
	}
}
